<?php
/**
 * 替换标签
 *
 * @param string	 $comments
 * @return string
 */
function __parse($tFile,$cFile,$path) {
	global $_G,$_L;
    $fileContent = false;

    if(!($fileContent = file_get_contents($tFile))){
        return false;
	}
	$fileContent = "<?php if(!defined('HAPPY')) exit('Access Denied'); ?>$fileContent";
    $fileContent = preg_replace("/\<\!\-\-\s*\\\$\{(.+?)\}\s*\-\-\>/ies", "__replace('<?php \\1; ?>')", $fileContent);
    $fileContent = preg_replace("/\{(\\\$[a-zA-Z0-9_\[\]\\\ \-\'\,\%\*\/\.\(\)\>\'\"\$\x7f-\xff]+)\}/s", "<?php echo \\1; ?>", $fileContent);
    $fileContent = preg_replace("/\\\$\{(.+?)\}/ies", "__replace('<?php echo \\1; ?>')", $fileContent);
    $fileContent = preg_replace("/\<\!\-\-\s*\{else\s*if\s+(.+?)\}\s*\-\-\>/ies", "__replace('<?php } else if(\\1) { ?>')", $fileContent);
    $fileContent = preg_replace("/\<\!\-\-\s*\{elif\s+(.+?)\}\s*\-\-\>/ies", "__replace('<?php } else if(\\1) { ?>')", $fileContent);
    $fileContent = preg_replace("/\<\!\-\-\s*\{else\}\s*\-\-\>/is", "<?php } else { ?>", $fileContent);
    for($i = 0; $i < 5; ++$i) {
        $fileContent = preg_replace("/\<\!\-\-\s*\{loop\s+(\S+)\s+(\S+)\s+(\S+)\s*\}\s*\-\-\>(.+?)\<\!\-\-\s*\{\/loop\}\s*\-\-\>/ies", "__replace('<?php if(is_array(\\1)){foreach(\\1 AS \\2=>\\3) { ?>\\4<?php }}?>')", $fileContent);
        $fileContent = preg_replace("/\<\!\-\-\s*\{loop\s+(\S+)\s+(\S+)\s*\}\s*\-\-\>(.+?)\<\!\-\-\s*\{\/loop\}\s*\-\-\>/ies", "__replace('<?php if(is_array(\\1)){foreach(\\1 AS \\2) { ?>\\3<?php }}?>')", $fileContent);
        $fileContent = preg_replace("/\<\!\-\-\s*\{if\s+(.+?)\}\s*\-\-\>(.+?)\<\!\-\-\s*\{\/if\}\s*\-\-\>/ies", "__replace('<?php if(\\1){?>\\2<?php }?>')", $fileContent);
    }
    $fileContent = preg_replace("#<!--\s*{\s*include\s+([^\{\}]+)\s*\}\s*-->#i", '<?php include template("\\1");?>', $fileContent);
	if(!include __taglibLanguage('taglib',$path)) {
				die("<br />Current template pack do not have a necessary language file ".__taglibLanguage('taglib')." or have syntax error!");
	}
	$fileContent = preg_replace("/\<\!\-\-\s*\{lang\s+(.+?)\}\s*\-\-\>/ies", "__languagevar('\\1')", $fileContent);
	//$fileContent = compress_html($fileContent);
	$_G['endTime'] = sprintf('%0.6f',sprintf('%.8f',array_sum(explode(' ', microtime()))) - $_G['startTime']);
    if(!file_put_contents($cFile,$fileContent)){
        return false;
	}
	@chmod($cFile,0777);
    return true;
}

 /**
* 压缩html : 清除换行符,清除制表符,去掉注释标记
* @param $string
* @return 压缩后的$string
* */
function compress_html($string) {
//$string = str_replace("\r\n", '', $string); //清除换行符
//$string = str_replace("\n", '', $string); //清除换行符
$string = str_replace(PHP_EOL, '', $string); 
$string = str_replace("\t", '', $string); //清除制表符
$pattern = array (
"/> *([^ ]*) *</", //去掉注释标记
"/[\s]+/",
"/<!--[^!]*-->/",
"/\" /",
"/ \"/",
"'/\*[^*]*\*/'"
);
$replace = array (
">\\1<",
" ",
"",
"\"",
"\"",
""
);
return preg_replace($pattern, $replace, $string);
} 

/**
 * 语言标签库标签
 *
 * @param string	 $comments
 * @return string
 */
function __taglibLanguage($name = 'taglib',$path){
	global $_G;
	return $path . '/' .  $name . '.lang' . '.php';
}

/**
 * 库标签
 *
 * @param string	 $comments
 * @return string
 */
function __languagevar($var) {
	if(isset($GLOBALS['_L'][$var])) {
		return $GLOBALS['_L'][$var];
	} else {
		return "!$var!";
	}
}
/**
 * 替换掉反斜线
 *
 * @param string	 $comments
 * @return string
 */
function __replace($string) {
	return str_replace("\\\"", "\"", preg_replace("/\[([a-zA-Z0-9_\-\.\x7f-\xff]+)\]/s", "['\\1']", $string));
}


/**
 * 模板函数原型
 *
 * @param string	 $comments
 * @return string
 */
function __template($tFile) {
	global $_G;
    $tFileN = preg_replace( '/\.html$/', '', $tFile);
    $tFile = DIR_TEMPLATE . '/' . $tFileN . '.html';
    $cFile = DIR_COMPILED . '/' . str_replace('/','_',$tFileN) . '.php';

    if(false===file_exists($tFile)){
        die("Templace File [$tFile] Not Found!");
    }

    if(false===file_exists($cFile)
            || @filemtime($tFile) > @filemtime($cFile)) {
        __parse($tFile,$cFile,DIR_TEMPLATE);
    }else{
		$_G['endTime'] = sprintf('%0.6f',sprintf('%.8f',array_sum(explode(' ', microtime()))) - $_G['startTime']);
	}

    return $cFile;
}

/**
 *加载模板函数
 *
 * @param string	 $comments
 * @return string
 */
function template($tFile) {
	return __template($tFile);
}


/**
 *打开页面
 *
 * @param string	 $comments
 * @return string
 */
function openurl($url) {
    $fp = @fopen($url, "r");
    $res = $fp ? 1 : 0;
    @fclose($fp);
}

/**
 *创建静态页
 *
 * @param string	 $comments
 * @return string
 */
function createhtml($file,$ispart=0) {
	$data = ob_get_contents();
	ob_end_clean();
	$strlen = file_put_contents($file, $data);
	@chmod($file, 0777);
	return $strlen;
}
?>